<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="是否上传完成" prop="isFinish">
        <el-select v-model="queryParams.isFinish" placeholder="请选择是否上传完成" clearable>
          <el-option
            v-for="dict in dict.type.is_finish"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:file:add']"
        >新增
        </el-button>
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="globalUpload"
        >全局上传
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="fileList">
<!--      <el-table-column label="分片上传的uploadId" align="center" prop="uploadId"/>-->
      <el-table-column label="md5" align="center" prop="fileMd5"/>
      <el-table-column label="文件名" align="center" prop="fileName"/>
      <el-table-column label="后缀" align="center" prop="fileExtension"/>
      <el-table-column label="桶名" align="center" prop="bucketName"/>
      <el-table-column label="文件存储路径" align="center" prop="objectName"/>
      <el-table-column label="文件大小" align="center" prop="fileSize">
        <template slot-scope="scope">
          {{scope.row.fileSize|byteConversion}}
        </template>
      </el-table-column>
      <el-table-column label="分片数量" align="center" prop="chunkNum"/>
      <el-table-column label="每个分片大小" align="center" prop="chunkSize">
        <template slot-scope="scope">
          {{scope.row.chunkSize|byteConversion}}
        </template>
      </el-table-column>
      <el-table-column label="图片">
        <template slot-scope="scope" v-if="scope.row.fileExtension=='png'">
          <el-image
            style="width: 100px; height: 100px"
            :src="getName(scope.row)"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="是否上传完成" align="center" prop="isFinish">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.is_finish" :value="scope.row.isFinish"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:file:remove']"
          >删除
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDownload(scope.row)"
          >下载
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="preview(scope.row)"
          >预览
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改文件上传对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="活动名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="活动区域">
          <el-select v-model="form.region" placeholder="请选择活动区域">
            <el-option label="区域一" value="shanghai"></el-option>
            <el-option label="区域二" value="beijing"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="文件">
          <minio-upload v-model="form.path" ></minio-upload>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">立即创建</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { listFile, delFile, addFile, updateFile } from '@/api/system/file'
import MinioUpload from '@/views/system/file/minioUpload.vue'
import Bus from '@/utils/bus'

export default {
  name: 'File',
  components: {  MinioUpload },
  dicts: ['is_finish'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 文件上传表格数据
      fileList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        uploadId: null,
        fileMd5: null,
        fileName: null,
        bucketName: null,
        objectName: null,
        fileSize: null,
        chunkSize: null,
        chunkNum: null,
        isFinish: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {
    this.getList()
  },
  filters:{
    // 字节单位转换
    byteConversion(size){
      if (size < 0.1 * 1024) {
        //小于0.1KB，则转化成B
        size = size.toFixed(2) + 'B'
      } else if (size < 0.1 * 1024 * 1024) {
        // 小于0.1MB，则转化成KB
        size = (size / 1024).toFixed(2) + 'KB'
      } else if (size < 0.1 * 1024 * 1024 * 1024) {
        // 小于0.1GB，则转化成MB
        size = (size / (1024 * 1024)).toFixed(2) + 'MB'
      } else {
        // 其他转化成GB
        size = (size / (1024 * 1024 * 1024)).toFixed(2) + 'GB'
      }
      // 转成字符串
      let sizeStr = size + '',
        // 获取小数点处的索引
        index = sizeStr.indexOf('.'),
        // 获取小数点后两位的值
        dou = sizeStr.substr(index + 1, 2)
      // 判断后两位是否为00，如果是则删除00
      if (dou == '00')
        return sizeStr.substring(0, index) + sizeStr.substr(index + 3, 2)
      return size
    }
  },
  methods: {
    //打开全局上传面板
    globalUpload(){
      Bus.$emit('openUploader', {})
    },
    getName(row){
      return process.env.VUE_APP_MINIO_URL+row.objectName
    },
    onSubmit(){
      console.log(this.form)
    },
    handleDownload(row) {
      this.download(`/system/file/download/${row.fileMd5}`, {}, row.fileName)
    },
    getList() {
      this.loading = true
      listFile(this.queryParams).then(response => {
        this.fileList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    cancel() {
      this.open = false
      this.reset()
    },
    reset() {
      this.form = {}
      this.resetForm('form')
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '文件上传'
    },
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFile(this.form).then(() => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addFile(this.form).then(() => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    handleDelete(row) {
      const id = row.id
      this.$modal.confirm('是否确认删除文件上传编号为"' + id + '"的数据项？').then(() => {
        return delFile(id)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    handleExport() {
      this.download('system/file/export', {
        ...this.queryParams
      }, `file_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
